﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SPIWareMVVM.ViewModels;
using DataAccess;

namespace SPIWareMVVM.Views.Commands
{
    public class AddNewWorkOrderCommand : BaseCommand
    {
        public AddNewWorkOrderCommand(BaseViewModel sender) : base(sender)
        {
            _sender = (ApplicationViewModel)sender;
        }

        public override void Execute(object parameter)
        {
            WorkOrderList list = (WorkOrderList)parameter;
            if (list != null)
            {
                list.lbWorkOrders.ItemsSource = null;
                list.dpWorkOrderEdit.DataContext = null;

                WorkOrder w = ApplicationData.NewWorkOrder();
                if (ApplicationData.SelectedCustomer != null) { w.Customer = ApplicationData.SelectedCustomer; w.CustomerId = ApplicationData.SelectedCustomer.CustomerId; }
                ((ApplicationViewModel)_sender).SelectedWorkOrder = w;

                if (ApplicationData.SelectedCustomer != null) list.lbWorkOrders.ItemsSource = ApplicationData.SelectedCustomer.WorkOrders;
                else list.lbWorkOrders.ItemsSource = ApplicationData.ocWorkOrders;

                if (ApplicationData.SelectedMaterial != null) w.Part = ApplicationData.SelectedMaterial;

                list.lbWorkOrders.SelectedItem = w;
                list.dpWorkOrderEdit.DataContext = w;

                if (list.Visibility == System.Windows.Visibility.Collapsed) list.Visibility = System.Windows.Visibility.Visible;
                list.dpWorkOrderEdit.Visibility = System.Windows.Visibility.Visible;
            }
        }
    }
}
